package camid.test.testobjects;

import java.io.Serializable;

import camid.distribution.lifecycle.LifecycleScope;
import camid.distribution.lifecycle.LocationScope;
import camid.distribution.lifecycle.ManagedObject;
import camid.util.testobjects.DelayUtil;

@ManagedObject ( lifecycleScope = LifecycleScope.PER_CLIENT, 
	locationScope = LocationScope.CLOUD )
public class StatefulDelayObject implements IDelayObject, Serializable
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3885894942500152639L;
	
	private long[] data;
	
	private int i;
	
	public StatefulDelayObject()
	{
		data = new long[32];
	}

	@Override
	public Long doDelay(Long delay) {
		
		delay = DelayUtil.gaussianDelay(delay);
		Long startTime = System.currentTimeMillis();
		Long endTime = System.currentTimeMillis();
		while(endTime - startTime < delay)
		{
			try 
			{
				Thread.sleep(100);
				endTime = System.currentTimeMillis();
			} 
			catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
		}
		data[i] = endTime - startTime;
		i = (i + 1) % 32;
		return new Long(i);
	}
	
	
}
